﻿Imports System.IO
Imports System.Xml
Imports System.Text
Public Class BlockOneHomeworkSectionFour
    Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Dim ArrayMax As Integer = 13 'One number past the end of actual info in array
    Dim intResponse, intResponse2, intResponse3 As Integer
    Dim strUser As String = "Enter Last Name"

    Shared Property MidiParent As MainForm

    Private Sub CloseToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub BlockOneSectionFour_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        txtObj.Text = "Objectives:" + vbCrLf + "- Given tower fall restraint devices and a restraint device inspection checklist, inspect the devices for safe condition and proper operation with no more than one instructor assist." _
            + vbCrLf + "- Given climbing equipment and fall restraint devices, ascend a tower to a height of 40 feet while using the proper climbing techniques and employing the correct climbing safety principles, with no more than one instructor assist." _
            + vbCrLf + "- Given climbing equipment, fall restraint devices, rope, and other appropriate equipment; rig, raise, and lower tools and equipment, using the correct knot or hitch, with no more than one instructor assist."
        If My.Settings.B1S1QC = 1 Then
            strUser = InputBox("Please enter last name", strUser)
            If strUser Is "" Then
                Me.Close()
            End If
        End If

        If My.Settings.B1S1QC > ArrayMax Then My.Settings.B1S1QC = ArrayMax

        If My.Settings.B1S4QC > 1 And My.Settings.B1S4QC < ArrayMax Then
            intResponse2 = MsgBox(" This assignment is already in progress. Do you want to continue?", 4, "Assignment in Progress")
            If intResponse2 = MsgBoxResult.Yes Then
                My.Settings.B1S4QC = My.Settings.B1S4QC - 1
                'Do Nothing
            ElseIf intResponse2 = MsgBoxResult.No Then
                intResponse = MsgBox("Do you want to start this over from the beginning?", 4, "Start back at the beginning?")
                If intResponse = MsgBoxResult.Yes Then
                    My.Settings.B1S4QC = 1
                ElseIf intResponse = MsgBoxResult.No Then
                    Me.Close()
                End If
            End If
        End If

        If My.Settings.B1S4QC = ArrayMax Then
            intResponse3 = MsgBox("This assignment has already been completed.  Are you sure you want to delete the current assignment and reaccomplish it?", 4, "Confirm Assignment Deletion")
            If intResponse3 = MsgBoxResult.Yes Then
                System.IO.File.Delete(filename & "\Homework\Block 1\Block 1 Section 4.txt")
                System.IO.File.Create(filename & "\Homework\Block 1\Block 1 Section 4.txt").Dispose()
                My.Settings.B1S4QC = 1
            ElseIf intResponse3 = MsgBoxResult.No Then
                Me.Close()
            End If
        End If

        If My.Settings.B1S4QC >= ArrayMax Then
            My.Settings.B1S4QC = 1
        End If
        GetXMLData()
    End Sub

    Private Sub btnNextQuestion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextQuestion.Click
        MainForm.strB1S4A(My.Settings.B1S4QC) = txtAnswer.Text '**** Linus 12 July ****
        My.Settings.B1S4QC = My.Settings.B1S4QC + 1
        txtAnswer.Clear()
        Next_Question()
    End Sub

    Public Sub Next_Question()
        txtAnswer.Focus()
        'LabelChangeCheck()
        If btnNextQuestion.Text = "Done" Then
            AddXMLData()
            WriteToFile()
            Me.Close()
            OpenResults()
        End If

        If My.Settings.B1S4QC = ArrayMax Then '**** Linus 12 July ***
            btnNextQuestion.Text = "Done"
        End If

        If My.Settings.B1S4QC <= ArrayMax Then '**** Linus 12 July ****
            txtQuestion.Text = MainForm.strB1S4Q(My.Settings.B1S4QC)
            txtAnswer.Text = MainForm.strB1S4A(My.Settings.B1S4QC)
        End If
    End Sub

    Private Sub WriteToFile()
        Dim Integrity As String = (filename) & "\Instructor\Block 1\Block 1 Section 4.txt"
        Dim Results As String = (filename) & "\Homework\Block 1\Block 1 Section 4.txt"
        Dim k As Integer = 0 '**** Linus 12 July ****

        If System.IO.File.Exists(filename & "\Homework\Block 1\Block 1 Section 4.txt") Then
            System.IO.File.Delete(filename & "\Homework\Block 1\Block 1 Section 4.txt")
            System.IO.File.Create(filename & "\Homework\Block 1\Block 1 Section 4.txt").Dispose()
        Else
            System.IO.File.Create(filename & "\Homework\Block 1\Block 1 Section 4.txt").Dispose()
        End If

        '***********Output to hidden file for Instructor Check***********
        If System.IO.File.Exists(Integrity) = True Then
            Dim hiddenWriter As New System.IO.StreamWriter(Integrity, True)
            hiddenWriter.WriteLine(DateTime.Now)
            hiddenWriter.WriteLine("- " & txtObj.Text)
            hiddenWriter.WriteLine()
            hiddenWriter.WriteLine("Student: " & strUser)
            hiddenWriter.WriteLine()
            For k = 1 To ArrayMax '**** Linus 12 July *****
                hiddenWriter.WriteLine("Question: " & MainForm.strB1S4Q(k))
                hiddenWriter.WriteLine("Answer: " & MainForm.strB1S4A(k))
                'hiddenWriter.WriteLine("Reference: " & MainForm.strB1S4R(k))
                hiddenWriter.WriteLine()
            Next k '***** Linus 12 July *****

            hiddenWriter.WriteLine("Block 1 Section 4 Complete")
            hiddenWriter.Close()

            'hiddenWriter.WriteLine(DateTime.Now) '**** Linus 12 July (was giving an error) ****
        End If

        '******Output to file for Student to review***********
        If System.IO.File.Exists(Results) = True Then
            Dim hiddenWriter As New System.IO.StreamWriter(Results, True)
            hiddenWriter.WriteLine("- " & txtObj.Text)
            hiddenWriter.WriteLine()
            hiddenWriter.WriteLine("Student: " & strUser)
            hiddenWriter.WriteLine()
            For k = 1 To ArrayMax '**** Linus 12 July ****
                hiddenWriter.WriteLine("Question: " & MainForm.strB1S4Q(k))
                hiddenWriter.WriteLine("Answer: " & MainForm.strB1S4A(k))
                hiddenWriter.WriteLine()
                'hiddenWriter.WriteLine("Reference: " & MainForm.strB1S4R(k))
                'hiddenWriter.WriteLine() '**** Linus (I think you ment comment this out) ****
            Next k '**** Linus 12 July ****
            hiddenWriter.WriteLine("Block 1 Section 4 Complete")
            hiddenWriter.Close()
        Else
            MsgBox("File Does Not Exist")
        End If
    End Sub

    Private Sub OpenResults()
        Dim p As Process = Process.Start((filename) & "\Homework\Block 1\Block 1 Section 4.txt")
    End Sub

    Private Sub btnPreviousQuestion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousQuestion.Click

        If My.Settings.B1S4QC = 0 Then Return '***** Linus 11 July *****

        If My.Settings.B1S4QC = ArrayMax Then '**** Linus 12 July *** Then
            btnNextQuestion.Text = "Next Question"
        End If

        My.Settings.B1S4QC = My.Settings.B1S4QC - 1

        txtQuestion.Text = MainForm.strB1S4Q(My.Settings.B1S4QC)
        txtAnswer.Text = MainForm.strB1S4A(My.Settings.B1S4QC) ' **** Linus 12 July ****
        txtAnswer.Focus()

    End Sub

    Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtAnswer.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            MainForm.strB1S4A(My.Settings.B1S4QC) = txtAnswer.Text
            My.Settings.B1S4QC = My.Settings.B1S4QC + 1
            Next_Question()
        End If
    End Sub

    Private Sub BlockOneHomeworkSectionOne_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        My.Settings.Save()
    End Sub

    Private Sub txtAnswer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAnswer.TextChanged
        txtAnswer.Text = txtAnswer.Text.Trim
    End Sub

    Public Sub GetXMLData()
        Dim xmlReader As New XmlTextReader("C:\Temp\B1S4AFSData.xml")
        Dim i As Integer = 1
        Dim y As Integer = 1
        xmlReader.Read()
        Do
            If xmlReader.Name.ToString() = "Answer" & i Then
                MainForm.strB1S4A(i) = xmlReader.ReadString().Replace("None", "")
                i = i + 1
            ElseIf xmlReader.Name.ToString() = "Question" & y Then
                MainForm.strB1S4Q(y) = xmlReader.ReadString()
                y = y + 1
            End If
            txtQuestion.Text = MainForm.strB1S4Q(1)
            txtAnswer.Text = MainForm.strB1S4A(1)

        Loop Until xmlReader.Read = False
        xmlReader.Close()
    End Sub

    Private Sub AddXMLData()
        Dim xd As New XmlDocument
        xd.Load("C:\Temp\B1S4AFSData.xml")
        Dim nod1 As XmlElement = xd.DocumentElement
        Dim nod2 As XmlElement = xd.DocumentElement
        Dim nod3 As XmlElement = xd.DocumentElement
        Dim nod4 As XmlElement = xd.DocumentElement
        Dim nod5 As XmlElement = xd.DocumentElement
        Dim nod6 As XmlElement = xd.DocumentElement
        Dim nod7 As XmlElement = xd.DocumentElement
        Dim nod8 As XmlElement = xd.DocumentElement
        Dim nod9 As XmlElement = xd.DocumentElement
        Dim nod10 As XmlElement = xd.DocumentElement
        Dim nod11 As XmlElement = xd.DocumentElement
        Dim nod12 As XmlElement = xd.DocumentElement
        Dim nod13 As XmlElement = xd.DocumentElement
        Dim nod14 As XmlElement = xd.DocumentElement

        Dim nod30, nod31, nod32, nod33, nod34, nod35, nod36, _
            nod37, nod38, nod39, nod40, nod41, nod42, nod43 As XmlNode

        nod30 = nod1.SelectSingleNode("Answer1")
        nod30.InnerText = MainForm.strB1S4A(1)
        nod31 = nod2.SelectSingleNode("Answer2")
        nod31.InnerText = MainForm.strB1S4A(2)
        nod32 = nod3.SelectSingleNode("Answer3")
        nod32.InnerText = MainForm.strB1S4A(3)
        nod33 = nod4.SelectSingleNode("Answer4")
        nod33.InnerText = MainForm.strB1S4A(4)
        nod34 = nod5.SelectSingleNode("Answer5")
        nod34.InnerText = MainForm.strB1S4A(5)
        nod35 = nod6.SelectSingleNode("Answer6")
        nod35.InnerText = MainForm.strB1S4A(6)
        nod36 = nod7.SelectSingleNode("Answer7")
        nod36.InnerText = MainForm.strB1S4A(7)
        nod37 = nod8.SelectSingleNode("Answer8")
        nod37.InnerText = MainForm.strB1S4A(8)
        nod38 = nod9.SelectSingleNode("Answer8")
        nod38.InnerText = MainForm.strB1S4A(8)
        nod39 = nod10.SelectSingleNode("Answer9")
        nod39.InnerText = MainForm.strB1S4A(9)
        nod40 = nod11.SelectSingleNode("Answer10")
        nod40.InnerText = MainForm.strB1S4A(10)
        nod41 = nod12.SelectSingleNode("Answer11")
        nod41.InnerText = MainForm.strB1S4A(11)
        nod42 = nod13.SelectSingleNode("Answer12")
        nod42.InnerText = MainForm.strB1S4A(12)
        nod43 = nod14.SelectSingleNode("Answer13")
        nod43.InnerText = MainForm.strB1S4A(13)

        xd.Save("C:\Temp\B1S4AFSData.xml")
    End Sub

    'Private Sub LabelChangeCheck()
    '    If My.Settings.B1S4QC < 3 Then
    '        lblSubject.Text = "Air Force Occupational Safety and Health"
    '    ElseIf My.Settings.B1S4QC < 15 Then
    '        lblSubject.Text = "Safety Hazards"
    '        lblSubject.TextAlign = ContentAlignment.MiddleCenter

    '    ElseIf My.Settings.B1S4QC > 15 Then
    '        lblSubject.Text = "Identify Standard Maintenance Practices"
    '    End If
    'End Sub
End Class